/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.click.app.dao.hibernate;

import com.click.app.dao.FeatureProductDao;
import com.click.app.model.FeatureProduct;
import com.click.app.model.Product;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author arnold
 */
public class FeatureProductDaoHibernate extends GenericDaoHibernate<FeatureProduct, Long> implements FeatureProductDao {
    public FeatureProductDaoHibernate() {
        super(FeatureProduct.class);
    }


    public List<FeatureProduct> findById(Long id) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List<FeatureProduct> findByProduct(Product product) {
        List<FeatureProduct> featureProducts = new ArrayList<FeatureProduct>();
        featureProducts.addAll(product.getListFeatureProducts());
        return featureProducts;
    }

    public List<FeatureProduct> findFeatureProducts() {
        List<FeatureProduct> dList = new ArrayList<FeatureProduct>();
        List<FeatureProduct> all = (List<FeatureProduct>) getHibernateTemplate().find("from FeatureProduct as d order by d.name");
        dList.add(all.get(0));
	String name = all.get(0).getName();
        for (int i = 1; i < all.size(); i++) {
		if(!all.get(i).getName().equals(name)){
			dList.add(all.get(i));
			name = all.get(i).getName();
		}
        }
        return dList;
    }

    public List<String> featureProductsNames() {
        return getHibernateTemplate().find("select distinct name from FeatureProduct");
    }

    public List<FeatureProduct> findFeatureProductsName(String name) {
        return getHibernateTemplate().find("from FeatureProduct where name=?", name);
    }

    public List<FeatureProduct> findFeatureProductsByDescription(String description) {
        return getHibernateTemplate().find("from FeatureProduct where description=?", description);
    }

    public List<FeatureProduct> findFeatureProductsByNameAndDescription(String name, String description){
        String[] args = {name, description};
        return getHibernateTemplate().find("from FeatureProduct where name=? and description=?", args);
    }

}
